package com.le.ui;

import com.le.component.BackGroundPanel;
import com.le.util.Query;
import com.le.util.ScreenUtils;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegistrationPage {
    JFrame jFrame = new JFrame("注册账号");
    String hostIpAddress;
    Font font = new Font("隶书", Font.PLAIN, 20);

    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images\\regist.jpg").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;

    JLabel name = new JLabel("用  户  名：");
    JLabel password = new JLabel("密      码：");
    JLabel confirmPassword = new JLabel("确 认 密 码：");

    JTextField nameTextField = new JTextField(15);
    JPasswordField passwordTextField = new JPasswordField(15);
    JPasswordField confirmTextField = new JPasswordField(15);

    JButton registered = new JButton("注册");
    JButton back = new JButton("返回登录界面");

    Connection connection;


    public RegistrationPage(String hostIpAddress) {
        this.hostIpAddress = hostIpAddress;
    }

    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jFrame.setIconImage(new ImageIcon("images//logo.png").getImage());

        name.setFont(font);
        password.setFont(font);
        confirmPassword.setFont(font);

        Box nameBox = Box.createHorizontalBox();
        Box passwordBox = Box.createHorizontalBox();
        Box confirmBox = Box.createHorizontalBox();
        Box genderBox = Box.createHorizontalBox();
        Box btnBox = Box.createHorizontalBox();

        nameBox.add(name);
        nameBox.add(Box.createHorizontalStrut(30));
        nameBox.add(nameTextField);

        passwordBox.add(password);
        passwordBox.add(Box.createHorizontalStrut(30));
        passwordBox.add(passwordTextField);

        confirmBox.add(confirmPassword);
        confirmBox.add(Box.createHorizontalStrut(20));
        confirmBox.add(confirmTextField);

        btnBox.add(Box.createHorizontalStrut(30));
        btnBox.add(registered);
        btnBox.add(Box.createHorizontalStrut(180));
        btnBox.add(back);

        Box content = Box.createVerticalBox();
        content.add(Box.createVerticalStrut(80));
        content.add(nameBox);
        content.add(Box.createVerticalStrut(30));
        content.add(passwordBox);
        content.add(Box.createVerticalStrut(30));
        content.add(confirmBox);
        content.add(Box.createVerticalStrut(30));
        content.add(genderBox);
        content.add(Box.createVerticalStrut(60));
        content.add(btnBox);

        backGroundPanel.add(content);



//        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));


        jFrame.add(backGroundPanel);

//        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理
        registered.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newName = nameTextField.getText().trim();
                String newPassword = new String(passwordTextField.getPassword());
                String confirmPassword = new String(confirmTextField.getPassword());

                if (!newPassword.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(jFrame, "两次密码输入不一致！");
                } else if (Query.checkUserNameExist(hostIpAddress, newName)) {
                    JOptionPane.showMessageDialog(jFrame, "用户名已存在！");
                } else {
                    PreparedStatement sql;
                    try {
                        Query query = new Query(hostIpAddress);
                        query.setDatabaseName("sams");
                        String uri = query.getURI();
                        connection = DriverManager.getConnection(uri, "zhang", "123456");
                        sql = connection.prepareStatement("insert into user values (?, ?)");
                        System.out.println(newPassword);
                        sql.setString(1, newName);
                        sql.setString(2, newPassword);
                        sql.execute();
                        JOptionPane.showMessageDialog(jFrame, "注册成功，即将跳转至登录界面。");
                        jFrame.dispose();
                        new MainInterface(hostIpAddress).init();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
                new MainInterface(hostIpAddress).init();
            }
        });
    }
}
